package com.jsj.controller;

import com.jsj.domain.Label;
import com.jsj.service.LabelService;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/label")
public class LabelController {
    private LabelService labelService;

    @Autowired

    public void setLabelService(LabelService labelService) {
        this.labelService = labelService;
    }

    /**
     * 删除标签
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public AjaxResult delete(@RequestBody int id) {
        if (labelService.delete(id) == 0) {
            return AjaxResult.error("删除标签失败");
        }
        return AjaxResult.success("删除标签成功");
    }

    /**
     * 创建标签
     */
    @RequestMapping(value = "/insert", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public AjaxResult insert(@RequestBody Label label) {
        if (labelService.insert(label) == 0) {
            return AjaxResult.error("创建标签失败");
        }
        return AjaxResult.success("创建标签成功");
    }

    /**
     * 修改标签
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public AjaxResult update(@RequestBody Label label) {
        if (labelService.update(label) == 0) {
            return AjaxResult.error("修改标签失败");
        }
        return AjaxResult.success("修改标签成功");
    }

    /**
     * 教师查询标签
     */
    @RequestMapping(value = "/queryByTeacher", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public List<Label> queryByTeacher(@RequestBody String creator) {
        return labelService.queryByTeacher(creator);
    }

    /**
     * 查询标签
     */
    @RequestMapping(value = "/queryById", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('student,teacher,admin')")
    public Label queryById(@RequestBody int labelId) {
        return labelService.queryById(labelId);
    }
}
